package com.humngyang.com.hmy.study.concurrency.myannoations.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


//将拦截器加入到容器中，需要在启动类中继承WebMvcConfigurationSupport实现其中的一个方法
public class HttpIntercepter extends HandlerInterceptorAdapter {

    private static Logger log = LoggerFactory.getLogger(HttpIntercepter.class);
    @Override
    //异常处理，正常的接口处理完之后也会走进来
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        //结果处理完之后调用remove方法
        ReuqestHolder.remove();
        log.info("afterCompletion");
//        System.out.println("afterCompletion");
        return;
    }

    //请求之前处理
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("preHandle ，{}");
//        System.out.println("preHandle");
        return true;
    }


    //请求之后处理
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
        return;
    }

    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        super.afterConcurrentHandlingStarted(request, response, handler);
    }

}
